package dao;

import help.Utils;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import dto.SettingDTO;

public class SettingDAO extends DatabaseHelper {
	
    public static final String TABLE_SETTING = "Setting";
    
    public static final String SETTING_ID = "ID";
    public static final String SETTING_STRESS = "Stress";
    public static final String SETTING_DEPRESSION = "Depression";
    public static final String SETTING_ANXIETY = "Anxiety";
    
	public SettingDAO(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

    public void addSetting(SettingDTO settingDTO) 
    {
    	// Open database
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(SETTING_STRESS, settingDTO.getStress());
	        values.put(SETTING_DEPRESSION, settingDTO.getDepression());
	        values.put(SETTING_ANXIETY, settingDTO.getAnxiety());
	        
	        // Inserting rows
	        open();
	        DatabaseHelper.myDataBase.insert(TABLE_SETTING, null, values);
	
	        // Close database connection
	        DatabaseHelper.myDataBase.close();
        }
        catch (Exception ex) {}
    }         
    
    public SettingDTO getSettingt(int id) {
        try {
	        Cursor cursor = DatabaseHelper.myDataBase.query(TABLE_SETTING, new String[] { SETTING_ID,
	        		SETTING_STRESS, SETTING_DEPRESSION, SETTING_ANXIETY}, SETTING_ID + "?",
	            new String[] {String.valueOf(id) }, null, null, null, null);

	        if (cursor != null)
	            cursor.moveToFirst();
	        
	        SettingDTO setting = new SettingDTO(
	                Integer.parseInt(cursor.getString(0)),
	                Integer.parseInt(cursor.getString(1)),
	                Integer.parseInt(cursor.getString(2)),
	                Integer.parseInt(cursor.getString(3)));
	        return setting;
        }
        catch (Exception e) {
        	return null;
        }
    }
        
    public List<SettingDTO> getAllSettings() {
    	try {
	        List<SettingDTO> settingList = new ArrayList<SettingDTO>();
	        
	        // Select all query
	        String selectQuery = "SELECT * FROM " + TABLE_SETTING;
	        open();
	        Cursor cursor = DatabaseHelper.myDataBase.rawQuery(selectQuery, null);
	        
	        // Looping through all rows and adding to list
	        
	        if (cursor.moveToFirst()) {
	            do {
	            	SettingDTO setting = new SettingDTO(
	    	                Integer.parseInt(cursor.getString(0)),
	    	                Integer.parseInt(cursor.getString(1)),
	    	                Integer.parseInt(cursor.getString(2)),
	    	                Integer.parseInt(cursor.getString(3)));
	            
	            	settingList.add(setting);
	            } while (cursor.moveToNext());
	        }
	        close();
	        return settingList;
    	}
    	catch (Exception e) {
			return null;
		}
    }
    
    public boolean updateSetting(SettingDTO settingDTO) {
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(SETTING_ID, settingDTO.getID());
	        values.put(SETTING_STRESS, settingDTO.getStress());
	        values.put(SETTING_DEPRESSION, settingDTO.getDepression());
	        values.put(SETTING_ANXIETY, settingDTO.getAnxiety());
	        open();
	        // Updating row
	        int isSucceed = DatabaseHelper.myDataBase.update(TABLE_SETTING, values, SETTING_ID + " = ?",
	                new String[] { String.valueOf(settingDTO.getID()) });
	        close();
	        if (isSucceed > 0) {
	        	return true;
	        }
	        	
	        return false;
        }
        catch (Exception ex) { 
        	return false; 
        }
    }
    
    public void deleteSetting(SettingDTO settingDTO) {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_SETTING, SETTING_ID + " = ?",
                new String[] { String.valueOf(settingDTO.getID()) });
    		DatabaseHelper.myDataBase.close();
    	}
    	catch (Exception ex) {}
    }
    
    public void deleteAllSettings() 
    {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_SETTING, null, null);
    	}
    	catch (Exception ex) {}
    }
    
    public int getSettingCount()
    {
    	try {
	        String countQuery = "SELECT * FROM " + TABLE_SETTING;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	        
	        // Return count
	        return cursor.getCount();
    	}
    	catch (Exception ex) {return -1;}
    }
}
